#!/usr/bin/python
# -*- coding: utf-8 -*-
import sync_baidu.sync_baidu
import os
from util.logger import process_log
from sync_baidu.sync_baidu import SyncException
__author__ = 'kongchao'

START_DATA = "start_data.lock"
END_DATA = "end_data.lock"


def monitor_data():
    init_monitor()
    write_file(START_DATA)
    try:
        sync_data = sync_baidu.sync_baidu.SyncData()
        sync_data.sync_data_call()
    except SyncException, se:
        process_log().error(se.message)
    except Exception, e:
        process_log().error(u"同步数据失败, %s" % e)
    finally:
        write_file(END_DATA)


def write_file(lock_file):
    try:
        lock_file = os.path.join(os.getcwd(), 'conf', lock_file)
        with open(lock_file, 'w') as f:
            pass
    except Exception, e:
        process_log().warning(u"写入文件失败,%s" % e)


def init_monitor():
    monitor_list = (START_DATA, END_DATA)
    try:
        for data_file in monitor_list:
            data_file = os.path.join(os.getcwd(),'conf',data_file)
            if os.path.exists(data_file):
                os.remove(data_file)
    except Exception, e:
        process_log().warning(u"初始化监控环境失败,%s" % e)

if __name__ == "__main__":
    monitor_data()
    # import  util.opends
    # opends = util.opends.OpenDS("123124135")
    # opends.tb_clean("134","123")






